package com.lw.extraceim.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.appcompat.widget.Toolbar;

import com.lw.extraceim.R;

public class ExtraceToolbar extends Toolbar {


    private static final String TAG = ExtraceToolbar.class.getSimpleName();

    private String titleText;

    public ImageView leftButton;

    public TextView titleTextView;

    public Button rightButton;

    private String rightButtonText;

    public ExtraceToolbar(Context context) {
        super(context);
        init(context, null);
    }

    public ExtraceToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(final Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.toolbar_layout, this, true);
        leftButton =  view.findViewById(R.id.toolbar_left_button);
        titleTextView =  view.findViewById(R.id.toolbar_title);
        rightButton =  view.findViewById(R.id.toolbar_right_button);
        //很重要
        setContentInsetsRelative(0, 0);

        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.ExtraceToolbar, 0, 0);
        titleText = a.getString(R.styleable.ExtraceToolbar_titleText);
        rightButtonText = a.getString(R.styleable.ExtraceToolbar_rightButtonText);
        Log.d(TAG, titleText);
        titleTextView.setText(titleText);
        rightButton.setText(rightButtonText);
        a.recycle();
    }

    public void setRightButtonText(String rightButtonText){
        if(rightButton != null){
            rightButton.setText(rightButtonText);
        }
    }
    public void setRightButtonTextByREsourceId(int rid){
        if (rightButton != null){
            rightButton.setText(rid);
        }
    }

    public void setTitle(String titleStr) {
        if (titleTextView != null) {
            titleTextView.setText(titleStr);
        }
    }

    public void setTitleByResourceId(int rid) {
        if (titleTextView != null) {
            titleTextView.setText(rid);
        }
    }

    public void setRightButtonImage(int resourceId) {
        if (rightButton != null) {
            rightButton.setBackgroundResource(resourceId);
        }
    }

    public void showRightButton() {
        if (rightButton != null) {
            rightButton.setVisibility(View.VISIBLE);
        }
    }

    public void hideRightButton() {
        if (rightButton != null) {
            rightButton.setVisibility(View.GONE);
        }
    }

    public void hide() {
        this.setVisibility(View.GONE);
    }

    public void setBackButtonOnClickListerner(OnClickListener listerner) {
        if (leftButton != null && listerner != null) {
            leftButton.setOnClickListener(listerner);
        }
    }

    public void setButtonOnClickListener(OnClickListener listener) {
        if (rightButton != null && listener != null) {
            rightButton.setOnClickListener(listener);
        }
    }

    public String getTitleText() {
        return titleText;
    }

    public void setTitleText(String titleText) {
        this.titleText = titleText;
    }

}
